package com.sls.game.parser;

import java.util.Iterator;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sls.core.util.ServerDateAndTime;
import com.sls.game.dao.eightooj.EightZeroJProductDAO;
import com.sls.game.dao.eightooj.EightZeroJRegionDAO;
import com.sls.game.pojo.eightooj.EightZeroJProduct;
import com.sls.game.pojo.eightooj.EightZeroJRegion;

public class ParseEightZeroJXml {

	private static Logger log = Logger.getLogger(ParseEightZeroJXml.class);

	@SuppressWarnings("unchecked")
	public static void insertProductInfo(String xml, EightZeroJProductDAO pDao) {
		Document document = null;
		Element root = null;
		try {
			document = DocumentHelper.parseText(xml);
			root = document.getRootElement();
			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				Element secondEle = it.next();
				String tagName = secondEle.getName();
				if ("ProductList".equals(tagName)) {
					saveProductList(pDao, secondEle);
				}
			}

		} catch (DocumentException e) {
			log.error("解析骏网返回的产品充值信息出现异常", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void insertRegionInfo(String xml, EightZeroJRegionDAO rDao) {
		Document document = null;
		Element root = null;
		try {
			document = DocumentHelper.parseText(xml);
			root = document.getRootElement();
			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				Element secondEle = it.next();
				String tagName = secondEle.getName();
				if ("GameRegionInfo".equals(tagName)) {
					Attribute categoryCodeAtt = secondEle.attribute("CategoryCode");
					Attribute categoryNameAtt = secondEle.attribute("CategoryName");
					Iterator<Element> it2 = secondEle.elementIterator();
					while (it2.hasNext()) {
						EightZeroJRegion eooj = new EightZeroJRegion();
						Element thirdEle = it2.next();
						String tagName2 = thirdEle.getName();
						if ("GameRegionItem".equals(tagName2)) {
							Iterator<Attribute> thirdAtt = thirdEle.attributeIterator();
							eooj.setCategoryCode(categoryCodeAtt.getValue());
							eooj.setCategoryName(categoryNameAtt.getValue());
							while (thirdAtt.hasNext()) {
								Attribute att = thirdAtt.next();
								String attName = att.getName();
								if ("RegionID".equals(attName)) {
									String regionId = att.getValue();
									eooj.setRegionId(regionId);
								} else if ("RegionName".equals(attName)) {
									String regionName = att.getValue();
									eooj.setRegionName(regionName);
								} else if ("RegionValue".equals(attName)) {
									String regionValue = att.getValue();
									eooj.setRegionValue(regionValue);
								} else if ("List_Order".equals(attName)) {
									String listOrder = att.getValue();
									eooj.setListOrder(listOrder);
								} 
							}
							insertOrUpdateRegion(eooj, rDao);
						}
					}
					
				}
			}

		} catch (DocumentException e) {
			log.error("解析骏网返回的区域信息出现异常", e);
		}
	}



	@SuppressWarnings("unchecked")
	private static void saveProductList(EightZeroJProductDAO pDao,
			Element secondEle) {
		Iterator<Element> it2 = secondEle.elementIterator();
		while (it2.hasNext()) {
			EightZeroJProduct eooj = new EightZeroJProduct();
			Element thirdEle = it2.next();
			String tagName2 = thirdEle.getName();
			if ("ProductItem".equals(tagName2)) {
				Iterator<Attribute> thirdAtt = thirdEle.attributeIterator();
				while (thirdAtt.hasNext()) {
					Attribute att = thirdAtt.next();
					String attName = att.getName();
					if ("CategoryCode".equals(attName)) {
						String categoryCode = att.getValue();
						eooj.setCategoryCode(categoryCode);
					} else if ("ProductCode".equals(attName)) {
						String productCode = att.getValue();
						eooj.setProductCode(productCode);
					} else if ("ProductFullName".equals(attName)) {
						String productFullName = att.getValue();
						eooj.setProductFullName(productFullName);
					} else if ("UseType".equals(attName)) {
						String useType = att.getValue();
						eooj.setUseType(useType);
					} else if ("SupplierID".equals(attName)) {
						String supplierId = att.getValue();
						eooj.setSupplierId(supplierId);
					} else if ("ParPrice".equals(attName)) {
						String parPrice = att.getValue();
						eooj.setParPrice(parPrice);
					} else if ("PurchasePrice".equals(attName)) {
						String purchasePrice = att.getValue();
						eooj.setPurchasePrice(purchasePrice);
					}
				}
				insertOrUpdatePro(eooj, pDao);
			}
		}
	}

	private static void insertOrUpdatePro(EightZeroJProduct eooj, EightZeroJProductDAO pDao) {
		EightZeroJProduct ep = pDao.queryByProductId(eooj.getProductCode());
		if (ep != null) {
			String id = ep.getId();
			eooj.setId(id);
			eooj.setUpdateTime(ServerDateAndTime.getNowDateTimeOfFormat());
			pDao.update(eooj);
		} else {
			String id = UUID.randomUUID().toString();
			eooj.setId(id);
			eooj.setUpdateTime(ServerDateAndTime.getNowDateTimeOfFormat());
			pDao.insert(eooj);
			
		}
	}
	
	private static void insertOrUpdateRegion(EightZeroJRegion eooj,
			EightZeroJRegionDAO rDao) {
		EightZeroJRegion er = rDao.queryByCCAndRI(eooj);
		if (er != null) {
			String id = er.getId();
			eooj.setId(id);
			eooj.setUpdateTime(ServerDateAndTime.getNowDateTimeOfFormat());
			rDao.update(eooj);
		} else {
			String id = UUID.randomUUID().toString();
			eooj.setId(id);
			eooj.setUpdateTime(ServerDateAndTime.getNowDateTimeOfFormat());
			rDao.insert(eooj);
		}
	}

}
